package com.socct.jetpackdemo


import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_main.*
import kotlinx.android.synthetic.main.fragment_main.view.*

/**
 * A simple [Fragment] subclass.
 */
class MainFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_main, container, false)
        initView(view)
        return view
    }

    private fun initView(view:View) {
        view.rv.layoutManager = LinearLayoutManager(context)
        view.rv.adapter = MyAdapter(ArrayList<ItemBean>().apply {
            add(ItemBean("room") {

            })

        })
    }


}


class MyAdapter(val list: List<ItemBean>) : RecyclerView.Adapter<MyViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.main_item, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val itemBean = list[position]
        val btn = holder.itemView.findViewById<Button>(R.id.btn)
        btn.text = itemBean.txt
        btn.setOnClickListener { itemBean.closure.invoke() }
    }


}


class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {


}